/*global define, alert */
define([
], 
function () {

    // CED subroutine for cleaning up JavaScript rounding errors 
    // to any reasonable number of decimal places 5/5/1997 last mod 2/19/2004
    // round for decimal of (value of precision) places, default is 3
    // This routine can be used to pass a number and a number for precision
    // or just a number only, that is to be rounded to a set number of decimal
    // places. This routine supports leading and trailing zeros, leading and
    // trailing spaces, and padding. To prevent errors, pass variables as a string.
    
    var perRound, zerosPad, convertDMS, convertDM, convertD; 
    
    perRound = function (num, precision) {
        
        var result1, result2, result3;
        // remark if passed from caller
    
        precision = parseInt(precision || 3, 10); // make certain the decimal precision is an integer
    
        result1 = num * Math.pow(10, precision);
        result2 = Math.round(result1);
        result3 = result2 / Math.pow(10, precision);
    
        return zerosPad(result3, precision);    
    };
    
    zerosPad = function (rndVal, decPlaces) {
        var valStrg, decLoc, totalPad, decPartLen, cntrVal;
        
        valStrg = rndVal.toString(); // Convert the number to a string
        decLoc = valStrg.indexOf("."); // Locate the decimal point

        // check for a decimal 
        if (decLoc === -1) {
    
            decPartLen = 0; // If no decimal, then all decimal places will be padded with 0s
            // If decPlaces is greater than zero, add a decimal point
            valStrg += decPlaces > 0 ? "." : "";
        } else {
            decPartLen = valStrg.length - decLoc - 1; // If there is a decimal already, only the needed decimal places will be padded with 0s
        }

        totalPad = decPlaces - decPartLen;    // Calculate the number of decimal places that need to be padded with 0s

        if (totalPad > 0) {

            // Pad the string with 0s
            for (cntrVal = 1; cntrVal <= totalPad; cntrVal = cntrVal + 1) {
    
                valStrg += "0";
    
            }
        }
        return valStrg;
    };
    
    convertDMS = function (deg, min, sec) {
        var dec_min;
        dec_min = (min * 1.0 + (sec / 60.0));
        return {
            dms: deg + "° " + min + "' " + sec + "\"",
            dm:  deg + "° " + perRound(dec_min, 3) + "'",
            d:   perRound(deg * 1.0 + (dec_min / 60.0), 6)
        };
    };  
    
    convertD = function (deg) {
        var gpsdeg, remainder, gpsmin, remainder2;
        gpsdeg = parseInt(deg, 10);
        remainder = deg - (gpsdeg * 1.0);
        gpsmin = remainder * 60.0;
        remainder2 = gpsmin - (parseInt(gpsmin, 10) * 1.0);
        remainder2 = remainder2 * 60.0;
        
        return {
            dms: gpsdeg + "° " + parseInt(gpsmin, 10) + "' " + perRound(remainder2, 3) + "\"",
            dm: gpsdeg + "° " + perRound(gpsmin, 5) + "'",
            d: deg
        };
        
    };
    
    convertDM = function (deg, min) {
        var sec, remainder;
        
        remainder = min - (parseInt(min, 10) * 1.0);
        
        return {
            dms: deg + "° " + parseInt(min, 10) + "' " + perRound(parseInt(remainder * 60, 10), 3) + "\"", 
            dm: deg + "° " + min + "'",
            d: perRound((deg * 1.0) + min / 60.0, 6)
        };
        
    };
    
    return {
        convertDMS: convertDMS,
        convertDM: convertDM,
        convertD: convertD
    };
    
});


